/**
 * 
 */
package net.mikoo.seals.agent.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import net.mikoo.seals.agent.model.Party;
import net.mikoo.seals.agent.model.UserLogin;

/**
 * @author huangxm
 *
 */
public class BaseController {
	
	protected static final String	RESULT_CODE	= "code";
	
	protected static final String	RESULT_MESSAGE	= "msg";
	
	protected static final String	ADMIN_VIEW	= "admin";
	
	protected static final String	FRONT_VIEW	= "front";
	
	protected static final String	MEMBER_VIEW	= "member";
	
	protected static final String	RESULT_VIEW	= "admin/result";
	
	protected static final String CARRIAGE_VIEW="carriage";
	
	protected static final String GEO_VIEW = "geo";
	
	protected static final String PRODUCT_CODE="product";
	
	
	protected static final String ERROR = "0";
	
	protected static final String SUCCESS= "1";
	
	
	protected static final String MSG_SUCCESS = "操作成功";
	protected static final String MSG_SYSTEM_ERROR = "系统出错！请联系管理员";
	
	//@Autowired
	//private ServletContext			servletContext;
	
	/**
	 * 站点模板跟目录
	 * @param websiteDir
	 * @param defaultStyle
	 * @return
	 */
	private String websiteResultBasePath(String websiteDir, String defaultStyle) {
		
		return websiteDir + "/template/" + defaultStyle + "/";
	}
	
	/**
	 * 站点模板视图
	 * @param websiteDir
	 * @param defaultStyle
	 * @param template
	 * @return
	 */
	public String websiteResultView(String websiteDir, String defaultStyle, String template) {
		
		return websiteResultBasePath(websiteDir, defaultStyle) + template;
	}
	
	/**
	 * 站点访问地址
	 * @param request
	 * @return
	 */
	public String getSiteURL(HttpServletRequest request) {
		String domain = request.getScheme() + "://" + request.getServerName() + "/";
		return domain;
	}
	
	protected String resultView(String viewDir, String path) {
		
		return viewDir + "/" + path;
	}
	
	protected String resultTips(String message, Model model) {
		model.addAttribute("message", message);
		return "admin/result/result_tips";
	}
	
	/**
	 * 跳转
	 * @param path
	 * @return
	 */
	protected String redirect(String path) {
		
		return "redirect:" + path;
	}
	
	protected String redirect(String path, Model model) {
		
		model.addAttribute("redirectUrl", path);
		return "redirect:" + path;
	}
	
	protected String forward(String path) {
		
		return "forward:" + path;
	}
	
	 
	
	/**
	 * 提示操作成功 跳转到指定页面
	 * @param path
	 * @param model
	 * @return
	 */
	protected String redirectTips(String path, Model model) {
		
		return redirectTips(path, null, model);
	}
	
	/**
	 * 操作成功提示指定信息，跳转到指定路径 ；message如果为空默认提示操作成功
	 * @param path
	 * @param message
	 * @param model
	 * @return
	 */
	protected String redirectTips(String path, String message, Model model) {
		
		model.addAttribute("redirectUrl", path);
		model.addAttribute("message", message);
		return RESULT_VIEW + "/redirect_tips";
	}
	
	/**
	 * 操作出错提示 不自动返回
	 * @param path
	 * @param message
	 * @param model
	 * @return
	 */
	protected String errorTips(String path, String message, Model model) {
		
		model.addAttribute("redirectUrl", path);
		model.addAttribute("message", message);
		return RESULT_VIEW + "/error_tips";
	}
	
	
	/**
	 * 弹出层操作成功后不提示直接关闭刷新当前页面
	 * @param model
	 * @param dialogId
	 * @return
	 */
	protected String dialogResult(Model model, String dialogId) {
		model.addAttribute("dialogId", dialogId);
		return RESULT_VIEW + "/dialog_result";
	}
	
	
	
	/**
	 * 
	 * @param model
	 * @param path
	 * @param dialogId
	 * @return
	 */
	protected String dialogRedirect(Model model, String path, String dialogId) {
		model.addAttribute("dialogId", dialogId);
		model.addAttribute("path", path);
		return RESULT_VIEW + "/dialog_redirect";
	}
	
	/**
	 * 弹出层操作成功后 提示操作成功 3秒后自动关闭
	 * @param dialogId
	 * @param model
	 * @return
	 
	protected String dialogTipsResult(String dialogId,Model model){
		
		return dialogTipsResult(dialogId,null,model);
	}*/
	
	/**
	 * 弹出层操作成功后 提示指定信息 3秒后自动关闭
	 * @param dialogId
	 * @param message
	 * @param model
	 * @return
	*/ 
	protected String dialogTipsResult(String dialogId,String message,Model model){
		model.addAttribute("message", message);
		return dialogResult(model,dialogId);
	}
	/**
	 * 弹出层返回直接关闭
	 * @param model
	 * @param dialogId
	 * @return
	 */
	
	/**
	 * 出口返回直接关闭
	 * @param model
	 * @return
	 */
	protected String winResult(Model model) {
		return RESULT_VIEW + "/win_result";
	}
	
	/**
	 * 获取用户登录信息
	 * @param request
	 * @return
	 */
	protected UserLogin getLoginUser(HttpServletRequest request,String sessionKey) {
		
		Party party = getParty(request,sessionKey);
		if(party != null){
			return party.getUserLogin();
		}
		return null;
	}
	
	/**
	 * 获取当前登录的当事人信息
	 * @param request
	 * @return
	 */
	protected Party getParty(HttpServletRequest request,String sessionKey) {
		
		HttpSession session = request.getSession();
		Object obj = session.getAttribute(sessionKey);
		if (obj != null) {
			Party party= (Party) obj;
			return party;
		}
		return null;
	}
	
	
	/**
	 * 获取当前当事人编号
	 * @param request
	 * @return
	 */
	protected Long getCurrentPartyId(HttpServletRequest request,String sessionKey) {
		UserLogin userLogin = getLoginUser(request,sessionKey);
		if(userLogin ==null){
			return null;
		}
		return userLogin.getPartyId();
	}
	
	protected String getCurrentUserLoginId(HttpServletRequest request,String sessionKey) {
		UserLogin userLogin = getLoginUser(request,sessionKey);
		return userLogin.getUserLoginId();
	}

	
	/**
	 * 保存session
	 * @param userLogin
	 * @param request
	 */
	protected void retainSession(Party party, HttpServletRequest request,String sessiond) {
		
		if (party != null) {
			HttpSession session = request.getSession();
			session.setAttribute(sessiond,party);
		}
	}
	
	/**
	 * 删除session
	 * @param userLogin
	 * @param request
	 */
	protected void removeSession(HttpServletRequest request,String sessiond) {
		HttpSession session = request.getSession();
		Object object = session.getAttribute(sessiond);
		if (object != null) {
			session.removeAttribute(sessiond);
		}
	}
	
	
	protected Map<String, Object> result(String code,String msg) {
		
		Map<String, Object> webResult = new HashMap<>();
		webResult.put(RESULT_CODE, code);
		webResult.put(RESULT_MESSAGE, msg);
		return webResult;
	}
	
	protected Map<String, Object> result(String code, String msg, Object data) {

		Map<String, Object> webResult = new HashMap<>();
		webResult.put(RESULT_CODE, code);
		webResult.put(RESULT_MESSAGE, msg);
		webResult.put("data", data);
		return webResult;
	}
	
	
	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
	    /**
	      * 自动转换日期类型的字段格式
	      */
	    binder.registerCustomEditor(Date.class, new SpecialDateEditor());
	}
	
	
}
